<?php
class item_uploadphoto extends template_item
{
	var $default_name = "Upload Your Photo";
	var $default_description = "Upload a photo for you to use. This item will bypass the group setting on uploading photo.";
	
	var $allow_multiple = 1;
	
	var $item_config = array();
	
	function load_item(&$item)
	{
		parent::load_item($item);
		
		if($item['item_config'] || $item['item_id'] != 0)
		{
			$this->item_config = unserialize($item['item_config']);
		}
		else
		{
			$this->item_config['upload_size'] = 50;
			$this->item_config['upload_width'] = 500;
			$this->item_config['upload_height'] = 500;
		}
	}
	
	function admin_form()
	{
		$html .= $this->ipsclass->adskin->add_td_row(array(
															"<b>Max upload file size (In KB)</b>",
															$this->ipsclass->adskin->form_input("item[".$this->item['item_id']."][upload_size]", $this->item_config['upload_size'])." KB<br />Max Width (px): ".$this->ipsclass->adskin->form_simple_input("item[".$this->item['item_id']."][upload_width]", $this->item_config['upload_width'])." Max Height (px): ".$this->ipsclass->adskin->form_simple_input("item[".$this->item['item_id']."][upload_height]", $this->item_config['upload_height']),
													)	);
		
		return $html;
	}
	
	function admin_save(&$item_info, $input)
	{
		$save = array();
		$save['upload_size'] = intval($input['upload_size']);
		$save['upload_height'] = intval($input['upload_height']);
		$save['upload_width'] = intval($input['upload_width']);
		
		$item_info['item_config'] = serialize($save);
	}
	
	function public_use_form($inventory_ids = array())
	{
		$this->ipsclass->load_language('lang_ucp');
		$this->ipsclass->load_language('lang_market_items');
		
		$hidden_fields = $this->_get_hidden_fields($inventory_ids);
		
		$this->ipsclass->lang['pph_max']  = sprintf( $this->ipsclass->lang['pph_max'], $this->item_config['upload_size'] );
		$this->ipsclass->lang['pph_max'] .= sprintf( $this->ipsclass->lang['pph_max2'], $this->item_config['upload_width'], $this->item_config['upload_height'] );
		
		$size_html = "";
		if($this->ipsclass->vars['disable_ipbsize'])
		{
			$size_html = <<<EOF
	<tr>
		<td class='row1' align='left'>
			&nbsp;
		</td>
		<td class='row2' align='right'>
			<b>{$this->ipsclass->lang['width']}</b>
			&nbsp;
			<input type='text' size='3" name='man_width' />
			&nbsp;&nbsp;
			<b>{$this->ipsclass->lang['height']}</b>
			&nbsp;
			<input type='text' size='3' name='man_height' /></td>
	</tr>
EOF;
		}
		else
		{
			$size_html = <<<EOF
	<tr>
		<td class='row1' align='left'>
			&nbsp;
		</td>
		<td class='row2' align='left'>
			<b>{$this->ipsclass->lang['pph_autosize']}</b>
			<br />
			({$this->ipsclass->lang['pph_as_desc']})
		</td>
	</tr>
EOF;
		}
		
		return <<<EOF
<div class='formsubtitle'>{$this->ipsclass->lang['upload_avatar']}</div>
<form action='{$this->base_url}' method='post' enctype='multipart/form-data'>
<input type='hidden' name='delete_photo' id='market-delete_photo' value='0' />
{$hidden_fields}
	<table class='ipbtable' cellspacing='0' cellpadding='0'>
		<tr>
			<td class='row1' align='left' colspan='2'>
				{$this->ipsclass->lang['pph_max']}
			</td>
		</tr>
		<tr>
			<td class='row1' align='left'>
				<b>{$this->ipsclass->lang['select_an_image_file']}</b>
			</td>
			<td class='row2' align='right'>
				<input type='file' size='30' name='upload_photo' />
			</td>
		</tr>
{$size_html}
		<tr>
			<td class='darkrow1' align='center' colspan='2'>
				<input type='submit' name='photo_upload' value='{$this->ipsclass->lang['upload_photo']}' class='forminput' />  <input type='submit' name='remove_photo' value='{$this->ipsclass->lang['remove_photo']}' class='forminput' onclick="document.getElementById('market-delete_photo').value=1;"/>
			</td>
		</tr>
	</table>
</form>
EOF;
	}
	
	function input_done()
	{
		return ( isset($this->ipsclass->input['photo_upload']) || isset($this->ipsclass->input['remove_photo']) ) && isset($this->ipsclass->input['delete_photo']) && isset($_FILES['upload_photo']) && is_uploaded_file($_FILES['upload_photo']['tmp_name']);
	}
	
	function public_use_item($inventory_ids = array()) 
	{
		$this->ipsclass->load_language('lang_market_items');
		
		require ROOT_PATH."sources/lib/func_usercp.php";
		$ucp = new func_usercp();
		$ucp->ipsclass = &$this->ipsclass;
		
		$this->ipsclass->member['g_photo_max_vars'] = $this->item_config['upload_size'].":".$this->item_config['upload_width'].":".$this->item_config['upload_height'];
		
		$photo = $ucp->lib_upload_photo();
		
		$this->ipsclass->DB->simple_construct( array( 'select' => 'pp_member_id',
													  'from'   => 'profile_portal',
													  'where'  => "pp_member_id=".$this->ipsclass->member['id'] ) );
		$this->ipsclass->DB->simple_exec();
	
		if ( $this->ipsclass->DB->get_num_rows() )
		{
			# Update...
			$this->ipsclass->DB->do_update( 'profile_portal', array( 'pp_main_photo'                => $photo['final_location'],
												  				   	 'pp_main_width'                => $photo['final_width'],
																   	 'pp_main_height'               => $photo['final_height'],
																	 'pp_thumb_photo'               => $photo['t_final_location'],
																	 'pp_thumb_width'               => $photo['t_final_width'],
																	 'pp_thumb_height'              => $photo['t_final_height'],
																 ), 'pp_member_id='.$this->ipsclass->member['id'] );
		}
		else
		{
			# Insert
			$this->ipsclass->DB->do_insert( 'profile_portal', array( 'pp_main_photo'                => $photo['final_location'],
												  				   	 'pp_main_width'                => $photo['final_width'],
																   	 'pp_main_height'               => $photo['final_height'],
																	 'pp_thumb_photo'               => $photo['t_final_location'],
																	 'pp_thumb_width'               => $photo['t_final_width'],
																	 'pp_thumb_height'              => $photo['t_final_height'],
																	 'pp_member_id'					=> $this->ipsclass->member['id'],
																 ) );
		}
		
		if ( $photo['status'] == 'fail' )
		{
			$this->ipsclass->load_language( 'lang_profile' );
			$this->ipsclass->Error(array('MSG'=>'pp_' . $photo['error']));
			return;
		}
		
		$this->delete_items($inventory_ids);
		
		$inv_id = reset($inventory_ids);
		$this->_write_log($inv_id, "Uploaded photo");
		
		$this->ipsclass->market_class->redirect('photo_updated',$this->ipsclass->base_url.'act=UserCP&CODE=photo',array('url'=>1));
	}
}
?>